package com.ruoyi.StarStudio.Service.Impl;

import com.ruoyi.StarStudio.DAO.GoodsDAO;
import com.ruoyi.StarStudio.DAO.GoodsStockDAO;
import com.ruoyi.StarStudio.Entity.Goods;
import com.ruoyi.StarStudio.Entity.GoodsChooseResult;
import com.ruoyi.StarStudio.Entity.GoodsStock;
import com.ruoyi.StarStudio.Service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.utils.SecurityUtils.getLoginUser;
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    GoodsDAO goodsDAO;

    @Autowired
    GoodsStockDAO goodsStockDAO;

    /**
     * 条件查询
     * @param goods 商品对象
     * @return List<Goods> 商品集合
     */
    @Override
    public List<Goods> findGoodsList(Goods goods) {
        //如果是超级管理员（银行），则可以查看所有商品
        if (getLoginUser().getUser().isAdmin()){
            return goodsDAO.findGoodsList(goods);
        }
        //如果是普通商家，只可以查看自己家的商品
        return goodsDAO.findGoodsListByNormalStore(goods,getLoginUser().getUserId());
    }

    /**
     * 根据商品ID删除商品
     * @param id 商品Id
     */
    @Override
    public void DeleteGoodsById(int id) {
        goodsDAO.DeleteGoodsById(id);
    }

    /**
     * 新增商品
     * @param goods 商品对象
     */
    @Override
    public void addGoods(Goods goods) {
        goods.setStore_id(getLoginUser().getUserId());
        goodsDAO.addGoods(goods);
        int stock = goods.getStock();
        //向商品库存个体表添加数据
        GoodsStock goodsStock = new GoodsStock();
        goodsStock.setGoods_id(goods.getGoods_id());
        goodsStock.setAdd_time(new Timestamp(new Date().getTime()));
        goodsStock.setHave_been_sold(false);
        goodsStock.setStore_id(getLoginUser().getUserId());
        goodsStock.setGoods_name(goods.getGoods_name());
        for (int i = 0; i < stock; i++) {
            goodsStockDAO.addGoodsStock(goodsStock);
        }
    }

    /**
     * 修改商品
     * @param goods 商品对象
     */
    public void updateGoods(Goods goods){
        int goodsNewStock = goods.getStock();
        int goodsOldStock = goodsDAO.findStockByGoodsId(goods.getGoods_id());
        if (goodsNewStock > goodsOldStock){  /*如果增加库存*/
            //向商品库存个体表添加数据
            GoodsStock goodsStock = new GoodsStock();
            goodsStock.setGoods_id(goods.getGoods_id());
            goodsStock.setAdd_time(new Timestamp(new Date().getTime()));
            goodsStock.setHave_been_sold(false);
            goodsStock.setStore_id(goods.getStore_id());
            goodsStock.setGoods_name(goods.getGoods_name());
            int addNum = goodsNewStock - goodsOldStock;
            for (int i = 0; i < addNum; i++) {
                goodsStockDAO.addGoodsStock(goodsStock);
            }
        }else if (goodsNewStock < goodsOldStock){ /*如果减少库存*/
            //删除多余商品库存
            int subNum = goodsOldStock - goodsNewStock;
            List<Long> goodsStockIds = goodsStockDAO.findGoodsStockIds(goods.getGoods_id(), goods.getStore_id());
            for (int i = 0; i < subNum; i++) {
                goodsStockDAO.deleteGoodsStock(Math.toIntExact(goodsStockIds.get(i)));
            }
        }
        goodsDAO.updateGoods(goods);
    }

    /**
     * 根据商品ID查找商品
     * @param id 商品ID
     * @return 商品对象
     */
    @Override
    public Goods findByGoods_id(long id) {
        return goodsDAO.findByGoods_id(id);
    }

    /**
     * 查询总记录数
     * @return 总记录数
     */
    @Override
    public long findAllRecordNumber() {
        if (getLoginUser().getUser().isAdmin()){
            return goodsDAO.findAllRecordNumber();
        }
        return goodsDAO.findAllRecordNumberForNormalStore(getLoginUser().getUserId());
    }

    @Override
    public Goods findByGoods_Item_Id(long goods_item_id) {
        return goodsDAO.findByGoodsItemId(goods_item_id);
    }

    @Override
    public List<GoodsChooseResult> getGoodsChooseResultList() {
        return goodsDAO.getGoodsChooseResultList();
    }
}
